//
//  AMNetworkMngTool.swift
//  Amicool
//
//  Created by Mac  on 2017/6/5.
//  Copyright © 2017年 Mac . All rights reserved.
//

import Foundation


//API
let AM_LOGIN = "http://amicool.neusoft.edu.cn/api.php/login"
let AM_LIST = "http://amicool.neusoft.edu.cn/api.php/lists"
typealias NetworkBlock = (_ flag:String)->Void
typealias NetworkVideoListBlock = (_ videoList:NSArray?)->Void
class  AMNetworkMngTool:NSObject{
    static  var shared = AMNetworkMngTool() //单例
    static var  loginReturnModel:LoginReturnJson?=LoginReturnJson()
    
    //登录请求
    func  AMNetwork_Login(_ parameters:NSDictionary,block:NetworkBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in
            parameters{
                let str = "\(key)=\(value)"
                paraArray.add(str)
        }
        
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LOGIN)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回的数据进行处理
            if data != nil {
                if let json = try?JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    AMNetworkMngTool.loginReturnModel = LoginReturnJson.LoginReturnJsonWithDict(json as? NSDictionary)
                    if AMNetworkMngTool.loginReturnModel?.error == nil {
                        //异步处理
                       block!("1")
                }
                    else {
                        block!("2")
                    }
                }
            }
            
        })
        dataTask.resume()//启动任务
    }
    //视频请求
    func  AMNetwork_GetVideoList(_ parameters:NSDictionary,block:NetworkVideoListBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in
            parameters{
                let str = "\(key)=\(value)"
                paraArray.add(str)
        }
        
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LIST)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回的数据进行处理
            if data != nil {
                if let json = try?JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    //将返回的数据封装成模型对象
                    var videoList = [AnyObject]()
                    for video in (json as! NSArray){
                        let model = VideoReturnJson.VideoReturnJsonWithDict(video as? NSDictionary)
                        videoList.append(model!)
                    }
                    //把视频列表返回给调用者
                   block!(videoList as NSArray)
                    }
                else {
                    block!(nil)
                }
            }
                else {
                    block!(nil)
                }
        })
        dataTask.resume()//启动任务
    }

}
